﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tst.Models;
using Ext.Direct.Mvc;

namespace Tst.Controllers
{
    public class CasoTesteController : Controller
    {
        private CasoTesteRepository casoTesteRep;
        private PlanoTesteRepository planoTesteRep;

        public CasoTesteController()
        {
            this.casoTesteRep = new CasoTesteRepository();
            this.planoTesteRep = new PlanoTesteRepository();
        }

        public ActionResult List()
        {
            return this.Direct(casoTesteRep.GetAll());
        }

        [FormHandler]
        public ActionResult Create(FormCollection request)
        {

            CasoTeste casoTeste;


            if (String.IsNullOrEmpty(request["cd_caso_teste"]))
            {
                casoTeste = new CasoTeste()
                {
                    ds_criterios = request.GetString("ds_criterios"),
                    ds_descricao = request.GetString("ds_descricao"),
                    ds_detalhamento = request.GetString("ds_detalhamento"),
                    ds_implementacao = request.GetString("ds_implementacao"),
                    ds_massa = request.GetString("ds_massa"),
                    ds_pos_condicoes = request.GetString("ds_pos_condicoes"),
                    ds_pre_condicoes = request.GetString("ds_pre_condicoes"),
                    PlanoTeste = planoTesteRep.GetById(request.GetInteger("cd_plano_teste"))
                };

                this.casoTesteRep.Insert(casoTeste);
            }
            else
            {
                casoTeste = this.casoTesteRep.GetById(Convert.ToInt16(request["cd_caso_teste"]));

                casoTeste.ds_criterios = request.GetString("ds_criterios");
                
                casoTeste.ds_descricao = request.GetString("ds_descricao");
                casoTeste.ds_detalhamento = request.GetString("ds_detalhamento");
                casoTeste.ds_implementacao = request.GetString("ds_implementacao");
                casoTeste.ds_massa = request.GetString("ds_massa");
                casoTeste.ds_pos_condicoes = request.GetString("ds_pos_condicoes");
                casoTeste.ds_pre_condicoes = request.GetString("ds_pre_condicoes");

                this.casoTesteRep.Update(casoTeste);

            }


            return this.Direct(new {
                success = true,
                cd_caso_teste = casoTeste.cd_caso_teste        
            });

        }

        public ActionResult Delete(int cd_caso_teste)
        {
            this.casoTesteRep.Delete(cd_caso_teste);
            this.casoTesteRep.Save();

            return this.Direct(new
            {
                success = true

            });
        }





    }
}
